package com.it.controller;

import com.it.bean.TopicBean;
import com.it.bean.UserBasicBean;
import com.it.service.TopicServcie;
import com.it.service.UserService;
import com.it.utils.StringUtil;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class UserController {
    public UserService userService;
    public TopicServcie topicServcie;

    public String login(HttpServletRequest request) throws Exception {
        String loginId = request.getParameter("loginId");
        String pwd = request.getParameter("pwd");
        UserBasicBean userBasicBean = userService.login(loginId, pwd);

        if (userBasicBean != null) {
            System.out.println("登录成功");
            request.getSession().setAttribute("userBasicBean", userBasicBean);
            return "user.do?operate=main";
        } else {
            System.out.println("登录失败");
            return null;
        }

    }

    /**
     * 首页
     *
     * @param request
     * @return
     * @throws Exception
     */
    public String main(HttpServletRequest request) throws Exception {
        UserBasicBean userBasicBean = (UserBasicBean) request.getSession().getAttribute("userBasicBean");
        String uid = request.getParameter("uid");
        if (StringUtil.isNotEmpty(uid)) {
            userBasicBean = userService.getUserBasicBeanById(uid);
        }

        //查询该用户的好友
        List<UserBasicBean> friendList = userService.getFriendListByUser(userBasicBean);
        //查询日志列表
        List<TopicBean> topicList = topicServcie.getTopicListByUid(userBasicBean.getId());
        request.getSession().setAttribute("topicList", topicList);
        request.getSession().setAttribute("friendList", friendList);
        request.getSession().setAttribute("nickName", userBasicBean.getNickName());
        request.getSession().setAttribute("friendId", userBasicBean.getId());
        return "main";

    }

}
